﻿using System.Threading;

namespace AsyncLibrary.StatusListener
{
  public class ControlDecoratorBase : SyncedStatusListener
  {
    private readonly ControlListenerBase _controlListener;
    private readonly AsyncOperation _op;

    public ControlDecoratorBase(ControlListenerBase listener, AsyncOperation op) 
      : base (listener, SynchronizationContext.Current)
    {
      _controlListener = listener;
      _op = op;
      op.OperationStarted += OnOperationStarted;
      op.OperationFinished += OnOperationFinished;
    }

    protected void OnOperationFinished()
    {
      _controlListener.TargetControl.Visible = false;
      _op.OperationStarted -= OnOperationStarted;
      _op.OperationFinished -= OnOperationFinished;
    }

    protected void OnOperationStarted()
    {
      _controlListener.TargetControl.Visible = true;
    }
  }
}